package cn.lp207663436.dynamicmanager.pojo.response;

import cn.lp207663436.dynamicmanager.pojo.enumerate.BaseEnum;
import cn.lp207663436.dynamicmanager.pojo.enumerate.SystemStatusEnum;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.ArrayUtils;

import java.beans.Transient;
import java.io.Serializable;
import java.util.Objects;

/**
 * 基本响应类
 */
public class BaseResponse implements Serializable {
	private static final long serialVersionUID = -3921982647867322414L;

	@ApiModelProperty(name = "code", value = "状态码", dataType = "Integer", example = "1")
	private Integer code;//状态码
	@ApiModelProperty(name = "message", value = "状态对应信息", dataType = "String", example = " 获取用户信息成功")
	private String message;//状态对应信息
	@ApiModelProperty(name = "data", value = "请求的数据结果", dataType = "json")
	private Object data;//请求的数据结果


	public BaseResponse() {
	}

	public BaseResponse(Integer code, String message) {
		this.code = code;
		this.message = message;
	}

	public BaseResponse(BaseEnum baseEnum) {
		this.code = baseEnum.getCode();
		this.message = baseEnum.getRemarks();
	}

	/**
	 * @param baseEnum 系统错误枚举
	 * @param addStr 拼接错误信息
	 */
	public BaseResponse(BaseEnum baseEnum, String addStr) {
		this.code = baseEnum.getCode();
		this.message = baseEnum.getRemarks() + addStr;
	}

	/**
	 * @param baseEnum 系统错误枚举
	 * @param addStr 拼接错误信息
	 * @param data 数据
	 */
	public BaseResponse(BaseEnum baseEnum, String addStr, Object data) {
		this.code = baseEnum.getCode();
		this.message = baseEnum.getRemarks() + addStr;
		this.data = data;
	}

	/**
	 * @param baseEnum 状态枚举
	 * @param data 数据
	 */
	public BaseResponse(BaseEnum baseEnum, Object data, Integer count) {
		this.code = baseEnum.getCode();
		this.message = baseEnum.getRemarks();
		this.data = data;
	}

	/**
	 * @return true 成功 false 其他
	 */
	@Transient
	public boolean isSuccess() {
		return Objects.equals(this.code, SystemStatusEnum.SUCCESS.getCode());
	}

	/**
	 * 构建业务错误信息返回结果
	 * @param message 错误信息
	 * @return
	 */
	public static BaseResponse buildError(String... message) {
		SystemStatusEnum fail = SystemStatusEnum.FAIL;
		if (ArrayUtils.isNotEmpty(message)) {
			return new BaseResponse(fail.getCode(), message[0]);
		}
		return new BaseResponse(fail);
	}

	/**
	 * 构建业务错误信息返回结果
	 * @param message 错误信息
	 * @return
	 */
	public static BaseResponse buildParamError(String... message) {
		SystemStatusEnum fail = SystemStatusEnum.PARAMETER_ERROR;
		if (ArrayUtils.isNotEmpty(message)) {
			return new BaseResponse(fail.getCode(), message[0]);
		}
		return new BaseResponse(fail);
	}

	/**
	 * 构建业务成功信息返回结果
	 * @param message 成功信息
	 * @return
	 */
	public static BaseResponse buildSuccess(String... message) {
		return buildSuccess(null, message);
	}

	/**
	 * 构建业务成功信息返回结果
	 * @param data 返回的数据
	 * @param message 成功信息
	 * @return
	 */
	public static BaseResponse buildSuccess(Object data, String... message) {
		SystemStatusEnum success = SystemStatusEnum.SUCCESS;
		if (ArrayUtils.isNotEmpty(message) && data != null) {
			return new BaseResponse(success, "," + message, data);
		}
		if (ArrayUtils.isNotEmpty(message)) {
			return new BaseResponse(success, "," + message[0]);
		}
		if (data != null) {
			return new BaseResponse(success, "", data);
		}
		return new BaseResponse(success);
	}

	/**
	 * 构建业务成功信息返回结果
	 * @param data 返回的数据
	 * @return
	 */
	public static BaseResponse buildSuccessData(Object data) {
		return new BaseResponse(SystemStatusEnum.SUCCESS, "", data);

	}

	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}
}
